package cn.stylefeng.guns.modular.api.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.api.entity.Materiel;
import cn.stylefeng.guns.modular.api.entity.TaskRecord;
import cn.stylefeng.guns.modular.api.mapper.TaskRecordMapper;
import cn.stylefeng.guns.modular.api.model.enums.CommonStatusEnum;
import cn.stylefeng.guns.modular.api.model.params.MaterielParam;
import cn.stylefeng.guns.modular.api.model.params.TaskRecordParam;
import cn.stylefeng.guns.modular.api.model.result.TaskRecordResult;
import cn.stylefeng.guns.modular.api.service.MaterielService;
import  cn.stylefeng.guns.modular.api.service.TaskRecordService;
import cn.stylefeng.guns.modular.api.vo.CheckStatusEnum;
import cn.stylefeng.guns.modular.api.vo.NameValueModel;
import cn.stylefeng.guns.modular.api.vo.RecordModel;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 任务记录 服务实现类
 * </p>
 *
 * @author samwuu
 * @since 2019-10-10
 */
@Service
public class TaskRecordServiceImpl extends ServiceImpl<TaskRecordMapper, TaskRecord> implements TaskRecordService {

    @Autowired
    private MaterielService materielService;

    @Override
    public void add(TaskRecordParam param){
        TaskRecord entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void saveAll(List<TaskRecord> taskRecords) {
        this.saveBatch(taskRecords);
    }

    @Override
    public void updateAll(List<TaskRecord> taskRecords) {
        this.updateBatchById(taskRecords);
    }

    @Override
    public void delete(TaskRecordParam param){
        this.baseMapper.deleteByParam(param);
    }

    @Override
    public void update(TaskRecordParam param){
        TaskRecord oldEntity = getOldEntity(param);
        TaskRecord newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);

        //减少物料
        String record = oldEntity.getRecord();
        try {
            if (param.getStatus() == CommonStatusEnum.DONE.getCode().intValue()){
                RecordModel object = JSONObject.parseObject(record, RecordModel.class);
                if (StringUtils.isNotEmpty(object.getMateriel())) {
                    List items = JSONObject.parseArray(object.getMateriel());
                    for (int i = 0; i < items.size(); i++) {
                        Map<String,String> m = (Map) items.get(i);
                        String materielId = m.get("name");
                        String number = m.get("number");
                        Materiel materiel = materielService.getById(materielId);
                        MaterielParam materielParam = new MaterielParam();
                        materielParam.setNumber(materiel.getNumber() - Integer.parseInt(number));
                        materielService.update(materielParam);
                    }
                }
            }

        }catch (Exception e){

        }
     }


    @Override
    public TaskRecordResult findBySpec(TaskRecordParam param){
        return this.baseMapper.customDetail(param);
    }

    @Override
    public List<TaskRecordResult> findListBySpec(TaskRecordParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(TaskRecordParam param){
        Page pageContext = getPageContext();
        if (pageContext.getSize()==0) {
            List datas =  this.baseMapper.customList(param);
            return LayuiPageFactory.createDataInfo(datas);
        }
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(TaskRecordParam param){
        return param.getTaskRecordId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private TaskRecord getOldEntity(TaskRecordParam param) {
        return this.getById(getKey(param));
    }

    private TaskRecord getEntity(TaskRecordParam param) {
        TaskRecord entity = new TaskRecord();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
